﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace JkUtils
{
    public class JkLogger
    {
        private static readonly string EventName = "JkLogs";
        public enum LogType
        {
            错误,
            警告,
            信息,
            调试
        }
        /// <summary>
        /// <para>　</para>
        /// 　常用工具类——系统日志类
        /// <para>　---------------------------------------------------</para>
        /// <para>　WriteEventLog：写入系统日志(２个方法重载)</para>
        /// <para>　DelEventName：删除日志事件源分类</para>
        /// </summary>
        #region 写入系统日志
        /// <summary>
        /// 写入系统日志
        /// </summary>
        /// <param name="EventName">事件源名称</param>
        /// <param name="LogStr">日志内容</param>
        public static void WriteEventLog(string LogStr)
        {
            Console.WriteLine(LogStr);
            try
            {
                if (!EventLog.SourceExists(EventName))
                {
                    EventLog.CreateEventSource(EventName, EventName);
                }
                EventLog.WriteEntry(EventName, LogStr);
            }
            catch
            {
            }
        }

        public static void WriteLogs(string content, LogType errorType = LogType.信息)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            if (!string.IsNullOrEmpty(path))
            {
                path += "\\Logs\\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (!File.Exists(path))
                {
                    using (FileStream fs = File.Create(path))
                    {
                        fs.Close();
                    }
                }
                if (File.Exists(path))
                {
                    using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\t" + errorType + "-->\n" + content);
                        sw.Close();
                    }
                }
            }
        }



        /// <summary>
        /// 写入系统日志
        /// </summary>
        /// <param name="EventName">事件源名称</param>
        /// <param name="LogType">日志类型</param>
        /// <param name="LogStr">日志内容</param>
        public static void WriteEventLog(string LogStr, EventLogEntryType LogType)
        {
            try
            {
                if (!EventLog.SourceExists(EventName))
                {
                    EventLog.CreateEventSource(EventName, EventName);
                }
                EventLog.WriteEntry(EventName, LogStr, LogType);
            }
            catch (Exception)
            {

            }
        }
        #endregion

        #region 删除日志事件源分类
        /// <summary>
        /// 删除日志事件源分类
        /// </summary>
        /// <param name="EventName">事件源名</param>
        /// <returns></returns>
        public static bool DelEventName()
        {
            bool flag = false;
            try
            {
                if (EventLog.SourceExists(EventName))
                {
                    EventLog.DeleteEventSource(EventName, ".");
                    flag = true;
                }
            }
            catch (Exception)
            {
            }
            return flag;
        }
        #endregion
    }

    public static class Log
    {
        private static readonly object _lock = new object();
        private static readonly string LogRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");

        static Log()
        {
            if (!Directory.Exists(LogRoot))
                Directory.CreateDirectory(LogRoot);
        }

        // 通用日志方法
        private static void WriteLog(string fileName, string message,
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            try
            {
                string filePath = Path.Combine(LogRoot, fileName);
                string callerFile = Path.GetFileName(sourceFilePath);
                string logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{callerFile}:{sourceLineNumber}] {message}{Environment.NewLine}";

                lock (_lock)
                {
                    File.AppendAllText(filePath, logMessage, Encoding.UTF8);
                }
            }
            catch
            {
                // 忽略日志写入异常
            }
        }

        public static void SearchLog(string message,
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            WriteLog("search.log", message, sourceFilePath, sourceLineNumber);
        }

        public static void RunLog(string message,
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            WriteLog("runtime.log", message, sourceFilePath, sourceLineNumber);
        }

        public static void ErrorLog(string message,
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            WriteLog("error.log", message, sourceFilePath, sourceLineNumber);
        }

        public static void RiskLog(string message,
            [CallerFilePath] string sourceFilePath = "",
            [CallerLineNumber] int sourceLineNumber = 0)
        {
            WriteLog("risk.log", message, sourceFilePath, sourceLineNumber);
        }
    }
}
